package com.example.tbtx.tbtx.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.example.tbtx.tbtx.utils.log.LogUtils;

public class FsLayoutmanager extends RecyclerView.LayoutManager {

    private final RecyclerView recyclerView;
    private final int rv_height;
    public int sideVisiableCount=3;
    public float scaleOffset=1.2f;
    private int rv_witdh;
    private int centerPosition;
    private View viewItem;
    private int offsetX;
    private int totalwidth =0;
    private int verticalScrollOffset;


    public FsLayoutmanager(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        rv_witdh = recyclerView.getMeasuredWidth();
        rv_height = recyclerView.getMeasuredHeight();

    }

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return null;
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        //在布局之前，将所有的子View先Detach掉，放入到Scrap缓存中
        detachAndScrapAttachedViews(recycler);

        centerPosition = getItemCount() / 2+offsetX/ 100;
        viewItem = recycler.getViewForPosition(centerPosition);
        measureChild(viewItem,0,0);

        drawLeft(recycler);
        drawRight(recycler);

        //画中间的
        addView(viewItem);
        layoutDecorated(viewItem,recyclerView.getMeasuredWidth()/2- viewItem .getMeasuredWidth()/2+offsetX,0,
                recyclerView.getMeasuredWidth()/2+viewItem .getMeasuredWidth()/2+offsetX ,
                viewItem.getMeasuredHeight());
//        scaleWithOffset(viewItem,3);

        totalwidth =getItemCount()*viewItem.getMeasuredWidth();


    }

    private void drawLeft(RecyclerView.Recycler recycler) {
        int leftUserd=recyclerView.getMeasuredWidth()/2- viewItem .getMeasuredWidth()/2-centerPosition*viewItem.getMeasuredWidth();
        for (int i = 0; i < centerPosition; i++) {
            View view = recycler.getViewForPosition(i);
            measureChildWithMargins(view, 0, 0);
            addView(view);
            int right = leftUserd + view.getMeasuredWidth();
            Log.d("fsssss", "drawLeft: left:"+leftUserd+" : "+right);
            layoutDecorated(view, leftUserd+offsetX,0,right+offsetX,view.getMeasuredHeight());
            leftUserd+=view.getMeasuredWidth();
//            scaleWithOffset(view,3-(centerPosition-i));
        }
    }

    private void drawRight(RecyclerView.Recycler recycler) {
        int rightUserd=recyclerView.getMeasuredWidth()/2+viewItem .getMeasuredWidth()/2+centerPosition*viewItem.getMeasuredWidth();
        for (int i = getItemCount()-1; i >centerPosition; i--) {
            View view = recycler.getViewForPosition(i);
            measureChildWithMargins(view, 0, 0);
            addView(view);
            int left=rightUserd-view.getMeasuredWidth();
            Log.d("fsssss", "drawRight:"+left+" : "+rightUserd);
            layoutDecorated(view, left+offsetX,0,rightUserd+offsetX,view.getMeasuredHeight());
            rightUserd-=view.getMeasuredWidth();
//            scaleWithOffset(view,3-(i-centerPosition));
        }
    }

    public void scaleWithOffset(View view,float offset){
        if (offset<0)
            return;
        view.setScaleY((float) Math.pow(scaleOffset,offset));
        view.setScaleX((float) Math.pow(scaleOffset,offset));
    }


    @Override
    public boolean canScrollHorizontally() {
        return true;
    }
    @Override
    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
        //列表向下滚动dy为正，列表向上滚动dy为负，这点与Android坐标系保持一致。
        //实际要滑动的距离
        int travel = dx;

        LogUtils.e("dy = " + dx);
        //如果滑动到最顶部
        if (verticalScrollOffset + dx < 0) {
            travel = -verticalScrollOffset;
//        } else if (verticalScrollOffset + dx > t - getVerticalSpace()) {//如果滑动到最底部
//            travel = totalHeight - getVerticalSpace() - verticalScrollOffset;
        }

        //将竖直方向的偏移量+travel
        verticalScrollOffset += travel;

        // 调用该方法通知view在y方向上移动指定距离
        offsetChildrenHorizontal(-travel);

        return travel;

    }


}
